#!/bin/bash
#
# Create new Subversion repository and import all files from ClearCase 
# repository to the created Subversion repository.
#
# Expected arguments:
# $1 - Path to a directory at which the Subversion repository should be created.
# $2 - Path to a ClearCase view directory to be imported. This argument is 
#      optional if CC_VIEW_DIR is specified in svncc.cfg or as environment variable.

################################# Functions ####################################

COMMAND=${COMMAND:-"$(basename "$0")"}

function print_help {
    echo '"Create new Subversion repository, import all files from ClearCase repository'
    echo '"to the created Subversion repository and generate svncc scripts and configs.'
    echo
    print_usage
}

function print_usage {
    echo "Usage: $COMMAND <svn dir> [cc dir] [options]"
    echo '    <svn dir>  : Path to a directory at which the Subversion repository should be created.'
    echo '    [cc dir]   : Path to a ClearCase view directory to be imported. This argument is'
    echo '                 optional if CC_VIEW_DIR is specified in svncc.cfg or as environment variable.'
    echo
    echo 'Options:'
    echo '    -r         : Root directory of the Subversion repository. Default is trunk.'
    echo '    -i         : Resources to be ignored. See the Subversion global-ignores configuration' 
    echo '                 parameter for details. Example:  -i "*~ *.swp *.rej".'
    echo '    -io        : Initialize only, i.e. nothing create and import but just geneate svncc scripts'
    echo '                 and configs in the specified, previously created, repository at <svn dir>.'
}

function create_svn_repo {
    echo "Creating Subversion repository at $SVN_REPO_DIR"
    "$SVNADMIN_CMD" create "$SVN_REPO_DIR"
}

function import_cc_repo {
    echo "Importing $CC_VIEW_DIR to $SVN_REPO_URL:"
    echo "$SVN_CMD ${SVN_ARGS[@]} import -m 'Initial import' '$CC_VIEW_DIR' '$SVN_REPO_URL'"
    "$SVN_CMD" "${SVN_ARGS[@]}" import -m "Initial import" "$CC_VIEW_DIR" "$SVN_REPO_URL"
}

function create_hook {
    local HOOK="${SVN_REPO_DIR}/hooks/$1"
    
    echo "Creating: $HOOK"
    echo '#!/bin/bash' > "$HOOK"
    echo >> "$HOOK"
    echo 'HOOKS_DIR=`dirname "$0"`' >> "$HOOK"
    echo >> "$HOOK"
    echo 'if [ "$OSTYPE" == "cygwin" ]' >> "$HOOK"
    echo 'then' >> "$HOOK"
    echo '    HOOKS_DIR=`cygpath -a "$HOOKS_DIR"`' >> "$HOOK"
    echo 'fi' >> "$HOOK"
    echo >> "$HOOK"
    echo '. "$HOOKS_DIR/../svncc/svncc.cfg"' >> "$HOOK"
    echo "SCRIPT_PATH=\"\$SVNCC_HOME/$1\"" >> "$HOOK"
    echo '. "$SCRIPT_PATH"' >> "$HOOK"
    chmod a+x "$HOOK"
    
    if [ ! -z "$IS_CYGWIN" ]
    then
        local HOOK_BAT="${SVN_REPO_DIR}/hooks/$1.bat"
        local BASH_LOCATION=$(cygpath -aw "$(which bash)")
        echo "Creating: $HOOK_BAT"
        echo "\"$BASH_LOCATION.exe\" --login -i \"%0/../$1\" \"%1\" \"%2\"" > "$HOOK_BAT"
    fi
}

function init_svncc_repo {
    local SVNCC_REPO_DIR="${SVN_REPO_DIR}/svncc"
    local SVNCC_REPO_CFG="${SVNCC_REPO_DIR}/svncc.cfg"
    
    # Creating hooks
    create_hook "pre-commit"
    create_hook "start-commit"
    
    # Creating svncc.cfg
    echo "Creating: $SVNCC_REPO_CFG"
    mkdir -p "$SVNCC_REPO_DIR"
    echo '# Path to ClearCase view directory.' > "$SVNCC_REPO_CFG"
    echo "CC_VIEW_DIR=\"$CC_VIEW_DIR\"" >> "$SVNCC_REPO_CFG"
    echo >> "$SVNCC_REPO_CFG"
    echo '# Path to directory with svncc scripts' >> "$SVNCC_REPO_CFG"
    echo "SVNCC_HOME=\${SVNCC_HOME:-\"$SVNCC_HOME\"}" >> "$SVNCC_REPO_CFG"
    echo >> "$SVNCC_REPO_CFG"
    echo '# Root directory of Subversion repository.' >> "$SVNCC_REPO_CFG"
    echo "SVN_REPO_ROOT=\${SVN_REPO_ROOT:-\"$SVN_REPO_ROOT\"}" >> "$SVNCC_REPO_CFG"
    
    if [ ! -z "$GLOBAL_IGNORES" ]
    then
            echo >> "$SVNCC_REPO_CFG"
            echo '# Patterns to be ignored by Subversion (global-ignores).' >> "$SVNCC_REPO_CFG"
            echo "SVN_IGNORES='$GLOBAL_IGNORES'" >> "$SVNCC_REPO_CFG"
    fi
    
    if [ ! -f "$CLEARTOOL_CMD" ]
    then
        CLEARTOOL_CMD=`which cleartool`
        
        if [ -f "$CLEARTOOL_CMD" ]
        then
              echo >> "$SVNCC_REPO_CFG"
              echo '# Path to cleartool command' >> "$SVNCC_REPO_CFG"
              echo "CLEARTOOL_CMD=\${CLEARTOOL_CMD:-\"$CLEARTOOL_CMD\"}" >> "$SVNCC_REPO_CFG"
        fi
    fi

    cd "$CC_VIEW_DIR"
    rm -rf .svn
    "$SVN_CMD" "${SVN_ARGS[@]}" co --force "$SVN_REPO_URL" .
}

################################################################################

if [[ $1 == help || $# == 0 ]]
then
      print_help
      exit 0  
fi

SVN_REPO_DIR="$1" && shift
[[ ! -z "$1" && "$1" != "-*" ]] && CC_VIEW_DIR="$1" && shift

for i in "$@"
do
    case "$i" in
        "-io") INIT_ONLY="true"; unset IS_ROOT; unset IS_IGNORE ;;
         "-i") IS_IGNORE="true"; unset IS_ROOT ;;
         "-r") IS_ROOT="true"; unset IS_IGNORE ;;
            *) 
             if [ ! -z "$IS_ROOT" ]
             then
                 SVN_REPO_ROOT="${i//\\//}"
                 [[ $SVN_REPO_ROOT == /* ]] && SVN_REPO_ROOT=${SVN_REPO_ROOT:1}
                 unset IS_ROOT
                 unset IS_IGNORE
             elif [ ! -z "$IS_IGNORE" ]
             then
                 GLOBAL_IGNORES="$i"
                 SVN_IGNORES="$i"
                 unset IS_ROOT
                 unset IS_IGNORE
             else
                 echo_err "Invalid option: $i"
                 print_usage
                 exit 1
             fi
                 
             ;;
         esac
done

SCRIPT_DIR=`dirname "$0"`
. "${SCRIPT_DIR}/svncc.cfg"


if [ -z "$SVN_REPO_DIR" ]
then
    echo_err "<svn dir> is not specified"
    print_usage
    exit 1
fi

if [ -z "$CC_VIEW_DIR" ]
then
    echo_err "[cc dir] is not specified"
    print_usage
    exit 1
fi

mkdir -p "$SVN_REPO_DIR"

if [ "$OSTYPE" != "cygwin" ]
then
    SVN_REPO_DIR=$(cd "$SVN_REPO_DIR"; pwd)
    CC_VIEW_DIR=$(cd "$CC_VIEW_DIR"; pwd)
else
    IS_CYGWIN="true"
    SVN_REPO_DIR=$(cygpath -a "$SVN_REPO_DIR")
    CC_VIEW_DIR=$(cygpath -am "$CC_VIEW_DIR")
fi

if [ ! -z "$INIT_ONLY" ] 
then
    [ ! -d "${SVN_REPO_DIR}/hooks" ] && echo_err "Subversion repository not found at $SVN_REPO_DIR" && exit 1
    init_svncc_repo
    exit
fi

SVN_REPO_URL="file://$SVN_REPO_DIR/$SVN_REPO_ROOT"

create_svn_repo
[ "$?" == "0" ] && import_cc_repo
[ "$?" == "0" ] && init_svncc_repo
